package com.zjbbkj.baibai.netty.core;

import com.zjbbkj.baibai.netty.filter.NettyServerInitFilter;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.ResourceLeakDetector;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.net.InetSocketAddress;

/**
 * Title: NettyServer
 * Description:
 * Netty服务端
 * Version:4.1.6V
 * @author wanli
 * @date 2021-08-24
 */

@Slf4j
@Component
public class NettyServerCoreStart implements ApplicationRunner {

    @Value("${server.netty.port}")
    private Integer port;

    @Override
    public void run(ApplicationArguments args){
        EventLoopGroup bossGroup = new NioEventLoopGroup(8);// 通过nio方式来接收连接和处理连接
        EventLoopGroup workGroup = new NioEventLoopGroup();
        InetSocketAddress address = new InetSocketAddress(port);
        try {
            ServerBootstrap b = new ServerBootstrap()
                    .group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(address)
                    .childHandler(new NettyServerInitFilter())
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.SIMPLE);
            //端口绑定，开始接收进来的连接
            ChannelFuture future = b.bind(port).sync();
            log.info("Netty服务已启动：" + address.getPort());
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            log.info("NettyServer服务端中断..." + e.getMessage());
            bossGroup.shutdownGracefully();   //关闭EventLoopGroup，释放掉所有资源包括创建的线程
            workGroup.shutdownGracefully();   //关闭EventLoopGroup，释放掉所有资源包括创建的线程
        }
    }

//    public void run() {
//        EventLoopGroup bossGroup = new NioEventLoopGroup(8);// 通过nio方式来接收连接和处理连接
//        EventLoopGroup workGroup = new NioEventLoopGroup();
//        InetSocketAddress address = new InetSocketAddress(port);
//        try {
//            ServerBootstrap b = new ServerBootstrap()
//                    .group(bossGroup, workGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .localAddress(address)
//                    .childHandler(new NettyServerInitFilter())
//                    .option(ChannelOption.SO_BACKLOG, 1024)
//                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
//                    .childOption(ChannelOption.SO_KEEPALIVE, true);
//
//            ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.SIMPLE);
//            //端口绑定，开始接收进来的连接
//            ChannelFuture future = b.bind(port).sync();
//            log.info("netty服务器开始监听端口：" + address.getPort());
//            future.channel().closeFuture().sync();
//        } catch (Exception e) {
//            log.info("server服务端异常。。。" + e);
//            e.getMessage();
//            bossGroup.shutdownGracefully();   //关闭EventLoopGroup，释放掉所有资源包括创建的线程
//            workGroup.shutdownGracefully();   //关闭EventLoopGroup，释放掉所有资源包括创建的线程
//        }
//    }
}
